package com.base;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.math.BigDecimal;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author gaoxing
 * @create 2018-09-17 下午2:30
 */
public class BaseMathUtils {
    private static Pattern CALC_PTN = Pattern.compile("([-+/*]?)[^-+/*]+");

    public BaseMathUtils() {
    }

    public static String multiplyTime(String src, Integer number) {
        if (StringUtils.isBlank(src)) {
            return null;
        } else {
            src = src.replaceAll("[\\s\\u00A0]{1,}", "");
            if (!BaseDateUtils.TIMES_PATTERN.matcher(src).matches()) {
                throw new IllegalArgumentException("无效的时间字符串:" + src);
            } else {
                StringBuilder buf = new StringBuilder();
                Matcher matcher = BaseDateUtils.TIME_PATTERN.matcher(src);

                while(matcher.find()) {
                    int value = Integer.valueOf(matcher.group(1)).intValue();
                    buf.append(value * number.intValue()).append(matcher.group(2));
                }

                if (buf.length() == 0) {
                    throw new IllegalArgumentException("无效的时间字符串:" + src);
                } else {
                    return buf.toString();
                }
            }
        }
    }

    public static Long yuan2Fen(BigDecimal yuan) {
        return yuan.multiply(new BigDecimal(100)).longValue();
    }

    public static BigDecimal fen2Yuan(Long fen) {
        return (new BigDecimal(fen.longValue())).divide(new BigDecimal(100));
    }

    public static BigDecimal calculate(Map<String, Pair<BigDecimal, BigDecimal>> feeMap, String src, String prefix) {
        if (StringUtils.isBlank(src)) {
            return null;
        } else {
            src = src.replaceAll("[\\s\\u00A0]{1,}", "");
            BigDecimal result = new BigDecimal(0);
            Matcher matcher = CALC_PTN.matcher(src);

            while(matcher.find()) {
                String operator = matcher.group(1);
                String item = matcher.group();
                if (StringUtils.isNotBlank(operator)) {
                    operator = matcher.group(1);
                    item = item.substring(1);
                } else {
                    operator = "+";
                }

                BigDecimal value = null;
                if (feeMap.containsKey(item)) {
                    value = (BigDecimal)((Pair)feeMap.get(item)).getLeft();
                } else if (StringUtils.isNotBlank(prefix) && item.startsWith(prefix)) {
                    item = item.substring(prefix.length());
                    if (feeMap.containsKey(item)) {
                        value = (BigDecimal)((Pair)feeMap.get(item)).getRight();
                    }
                }

                if (value == null) {
                    throw new IllegalArgumentException("无法在" + feeMap.toString() + "中找到'" + item + "'对应的值");
                }

                byte var9 = -1;
                switch(operator.hashCode()) {
                    case 42:
                        if (operator.equals("*")) {
                            var9 = 1;
                        }
                        break;
                    case 45:
                        if (operator.equals("-")) {
                            var9 = 0;
                        }
                        break;
                    case 47:
                        if (operator.equals("/")) {
                            var9 = 2;
                        }
                }

                switch(var9) {
                    case 0:
                        result = result.subtract(value);
                        break;
                    case 1:
                        result = result.multiply(value);
                        break;
                    case 2:
                        result = result.divide(value);
                        break;
                    default:
                        result = result.add(value);
                }
            }

            return result;
        }
    }
}
